layui.use(['form','layer'], () =>(function ({form,layer,starttime,isSandMobileCaptcha,url,contentType,ok}) {

    if (window != top){
        top.location.href = location.href;
    }
    form.verify({
        mubile: function(value, item){ //value：表单的值、item：表单的DOM对象
            if (!/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test(value)){
                return '手机格式不正确';
            }
        }
    }),
    form.render(),

    $(".beforMobileCaptcha").click(function () {
        $("input[name=captcha]").attr("lay-verify",""),
        $('.sandMobileCaptchaBtn').click(),
        isSandMobileCaptcha=true
    }),
    $(".beforlogin").click(function () {
        $("input[name=captcha]").attr("lay-verify","required"),
        $('.login').click()
    }),
    form.on('submit(sandMobileCaptchaBtn)',({elem,field})=>{
        if (starttime==60){
            $.ajax({
                type:'post',
                url:'/security/mobile/getMobileCapcha',
                contentType:"application/json;charset=utf-8",
                data:JSON.stringify(field),
                success:function (res) {
                    if(res.isok && res.code==200){
                        let interval = setInterval(function () {
                            starttime--;
                            $(".beforMobileCaptcha").html( starttime+ " 秒");
                            if(starttime==0){
                                $(".beforMobileCaptcha").html("重新发送");
                                clearInterval(interval)
                                starttime=60
                            }
                        }, 1000);
                        layer.alert(res.data)
                    }else {
                        layer.msg(res.message)
                    }
                }
            });
        }else {
            layer.msg("不允许重复发送")
        }
        return false;
    })

    form.on('submit(login)',({field})=>{
        isSandMobileCaptcha?(
            starttime<60?(
                $.ajax({
                    type:'post',
                    url:url,
                    data:field,
                    success:(data) => (
                        console.log(data),
                        data.isok && data.code=="200"?(window.location.href=data.data ):ok(data.message,5)
                    )
                })
            ):(
                layer.msg('验证码已过期,请重新获取验证码')
            )
        ):(
            layer.msg('请获取验证码')
        )
        $("input[name=captcha]").attr("lay-verify","required")
        return false;
    })

})(
    {
        form: layui.form,
        layer: layui.layer,
        starttime:60,
        isSandMobileCaptcha:false,
        url: "/security/mobileLogin",
        contentType: 'application/json;charset=utf-8',
        ok: function (msg, icon) {
            layui.layer.confirm(msg, {
                id: "ok",
                icon: icon,
                anim: 4,
                title: '提示',
                btn: ['确定'],
                closeBtn: false,
                success: function (layero) {
                    var btn = layero[0].getElementsByClassName('layui-layer-btn')[0].getElementsByTagName('A')[0];
                    btn.href = 'javascript:void(0)';
                    btn.focus();
                },
                yes: function (index, layero) {
                    layer.close(index); //再执行关闭
                },
                end: function () {

                }
            });
        },
    }
))



